package com.gujiangbo.multithreadskill;

/**
 * @author gujiangbo
 */
public class ThreadMethod {
    public static void main(String[] args) {
        SleepThread sleepThread = new SleepThread();
        sleepThread.setName("睡眠线程");
        System.out.println(System.currentTimeMillis());
        sleepThread.start();
        System.out.println(System.currentTimeMillis());
        //获取线程唯一标识
        long id = sleepThread.getId();
        System.out.println("当前线程标识:" + id);

        //线程退出
        StopThread stopThread = new StopThread();
        stopThread.start();
        //stopThread.stop();
        stopThread.interrupt();//中断
    }
}

class SleepThread extends Thread {

    @Override
    public void run() {
        System.out.println("start sleep ..");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end sleep ..");
    }
}

class StopThread extends Thread {
    @Override
    public void run() {
        while (true) {
            if (this.isInterrupted()) {
                System.out.println("当前线程是停止状态，需要退出");
                //break;
                return;
            }
        }
    }
}